# --------------------------------------------------------------------------
# 检查用户名
import re

# username = input("input your name: ")
username = ['fa_', '34adf_', '_afdf22', 'fad342ad21213121sdfsdsd', 'fad342ad_']


def ree(x):
    return re.findall(r"^[a-zA-Z][\w ]{5,17}$", x)


print(list(filter(lambda item: len(item) != 0, map(ree, username))))
print()


# ---------------------------------------------------------------------------
# 匿名函数+reduce实现n的阶乘
from functools import reduce
print(reduce(lambda x, y: x * y, range(1, 3)))
print(reduce(lambda x, y: x * y, range(1, 4)))
print(reduce(lambda x, y: x * y, range(1, 5)))
print()

# ---------------------------------------------------------------------------
# filter函数：找出[1-100]能被3整除的数
print(list(filter(lambda item: item % 3 == 0, range(1, 101))))
print()

# ---------------------------------------------------------------------------
# map函数:求list[1-10]各项的平方
print(list(map(lambda item: item**2, range(1, 11))))
print()


# ---------------------------------------------------------------------------
# 将列表[1, 4, 5, 9]转换成'1459'字符串
a = [1, 4, 5, 9, 1]
b = {}
for i in a:
    b[i] = str(i)
print(b)
print(reduce(lambda x, y: x + y, list(map(lambda item: b[item], a))))
print(reduce(lambda x, y: str(x) + str(y), a))
print()


# ---------------------------------------------------------------------------
# 列表正负整数排序
list1 = [7, -8, 5, 4, 0, -2, -5]


def func(x):
    if x < 0:
        return abs(x) + 10
    else:
        return x


print(sorted(list1, key=func))
print()


# ---------------------------------------------------------------------------
# 字符串排序
s = "asbf234GDSdsf23"


def func1(x):
    if 48 <= ord(x) <= 57:
        if ord(x) % 2 == 0:
            return ord(x)
        else:
            return ord(x) - 10
    elif 97 <= ord(x) <= 122:
        return ord(x) - 100
    else:
        return ord(x) - 52


print(sorted(s, key=func1))
print(list(map(func1, sorted(s, key=func1))))
